#include <fstream>

#include "Reader.h"

Reader::Reader(const std::string &filename) :
_filename(filename)
{
}

Model Reader::read() const
{
	std::ifstream input(this->_filename.c_str());

	City city(this->read_city(input));

	Trajectory trajectory(this->read_trajectory(input));

	double threshold(this->read_threshold(input));

	Model model(city, trajectory, threshold);

	return model;
}

unsigned int Reader::rock_count(std::ifstream &input) const
{
	unsigned int n(0);

	input >> n;

	return n;
}

Vector Reader::read_vector(std::ifstream &input) const
{
	double x(0);
	double y(0);
	double z(0);

	input >> x;
	input >> y;
	input >> z;

	return Vector(x, y, z);
}

std::vector< Vector > Reader::read_rocks(std::ifstream &input, unsigned int count) const
{
	std::vector< Vector > rocks;

	for (unsigned int index = 0; index < count; ++index)
	{
		const Vector &rock(this->read_rock(input));

		rocks.push_back(rock);
	}

	return rocks;
}

City Reader::read_city(std::ifstream &input) const
{
	unsigned int n(this->rock_count(input));
	std::vector< Vector > rocks(this->read_rocks(input, n));

	return City(rocks);
}

Trajectory Reader::read_trajectory(std::ifstream &input) const
{
	const Vector &a(this->read_vector(input));
	const Vector &b(this->read_vector(input));

	return Trajectory(a, b);
}

double Reader::read_threshold(std::ifstream &input) const
{
	double threshold(0);

	input >> threshold;

	return threshold;
}
